package com.black.knight.chess;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;

import com.black.knight.chess.calls.CheckUsernameCall;
import com.black.knight.chess.calls.RegisterUserCall;
import com.black.knight.chess.common.User;
import com.black.knight.chess.model.SettingsModel;
import com.black.knight.chess.utils.Utils;

public class RegistrationActivity extends Activity{

	private RegistrationActivity context;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        
    	this.context = this;
    	
    	Utils.setLanguage(SettingsModel.getInstance().getLanguage(), context);
    	
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.register);
        
        setTitle(context.getResources().getString(R.string.registration));
        
        final EditText usernameInputField = (EditText) findViewById(R.id.usernameInputField);
        final EditText passwordInputField = (EditText) findViewById(R.id.passwordInputField);
        final EditText passwordAgainInputField = (EditText) findViewById(R.id.passwordAgainInputField);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        
        final ImageButton registerButton = (ImageButton) findViewById(R.id.registerButton);
        
        usernameInputField.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(usernameInputField.getText().toString().length() > 3) {
					CheckUsernameCall call = new CheckUsernameCall(s.toString(), context);
					
					SettingsModel.EXECUTOR_SERVICE.execute(call);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
        
        registerButton.setOnClickListener(new View.OnClickListener() { 
			@Override
			public void onClick(View v) {
				String username = usernameInputField.getText().toString();
				String password = passwordInputField.getText().toString();
				String passwordAgain = passwordAgainInputField.getText().toString();
				
				if(username.length() < 6) {
					usernameInputField.setError(context.getResources().getString(R.string.username_too_short));					
				} else {
					usernameInputField.setError(null);
				}
				
				if(password.length() < 6) {
					passwordInputField.setError(context.getResources().getString(R.string.password_too_short));					
				} else {
					passwordInputField.setError(null);
				}
				
				if(!password.equals(passwordAgain)) {
					passwordAgainInputField.setError(context.getResources().getString(R.string.password_dont_match));
				} else {
					passwordAgainInputField.setError(null);
				}
				
				if(usernameInputField.getError() == null && passwordInputField.getError() == null && passwordAgainInputField.getError() == null) {
					User user = new User();
					user.setUsername(username);
					user.setPassword(password);
					
					progressBar.setVisibility(ProgressBar.VISIBLE);
					
					RegisterUserCall call = new RegisterUserCall(user, context);
					
					SettingsModel.EXECUTOR_SERVICE.execute(call);
				}
			}
		});
    }
    
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        Utils.setLanguage(SettingsModel.getInstance().getLanguage(), context);
      
      super.onConfigurationChanged(newConfig);
    }
}